package com.mobilepay.moneypig.model.activity.launcher;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.base.NoDoubleClickListener;
import com.mobilepay.moneypig.config.Constant;
import com.mobilepay.moneypig.config.Key;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.timecount.CodeTimerService;
import com.mobilepay.moneypig.timecount.CountDownTimerUtil;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;

/**
 * Created by xlm on 2017/7/13 0013
 */

public class ForgetActivity extends BaseActivity {
    private static final String TAG = "ForgetActivity";
    private TextView mTextViewForget;
    private EditText mMobile, mYzcode, mPassWord;
    private ImageView mImageBack;
    private Button mButtonChange;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_forget);
    }

    @Override
    protected void afterSetContentView() {
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#40000000"));
    }

    @Override
    protected void initViews() {
        mTextViewForget = (TextView) findViewById(R.id.forget_button);
        mMobile = (EditText) findViewById(R.id.forget_mobile);
        mYzcode = (EditText) findViewById(R.id.forget_code);
        mPassWord = (EditText) findViewById(R.id.forget_pw);
        mImageBack = (ImageView) findViewById(R.id.forget_back);
        mButtonChange = (Button) findViewById(R.id.forget_change);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }

    @Override
    protected void onSetListener() {
        mImageBack.setOnClickListener(this);
        mTextViewForget.setOnClickListener(this);
        mButtonChange.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                super.onNoDoubleClick(view);
                onChangePassWd();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.forget_button) {
            getYzCode();
        } else if (v.getId() == R.id.forget_back) {
            finish();
        }
    }

    // 注册广播
    private static IntentFilter updateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CodeTimerService.IN_RUNNING);
        intentFilter.addAction(CodeTimerService.END_RUNNING);
        return intentFilter;
    }

    String sMobile;
    // 广播接收者
    private final BroadcastReceiver mUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            switch (action) {
                case CodeTimerService.IN_RUNNING:
                    if (mTextViewForget.isEnabled())
                        mTextViewForget.setEnabled(false);
                    // 正在倒计时
                    sMobile = intent.getStringExtra("mobile");
                    mMobile.setText(sMobile);
                    mMobile.setSelection(sMobile.length());
                    mTextViewForget.setText(intent.getStringExtra("time"));
                    break;
                case CodeTimerService.END_RUNNING:
                    // 完成倒计时
                    mTextViewForget.setEnabled(true);
                    mTextViewForget.setText("获取验证码");
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        // 注册广播
        registerReceiver(mUpdateReceiver, updateIntentFilter());
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 移除注册
        unregisterReceiver(mUpdateReceiver);
    }

    /**
     * 获取验证码
     */
    private void getYzCode() {
        final String mobile = mMobile.getText().toString();
        if (CommonUtil.isEmpty(mobile)) {
            showToastShort("请输入手机号码");
            return;
        }
        if (!CommonUtil.isMobile(mobile)) {
            showToastShort("请输入正确的手机号码");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.sendsms);
        param.add("mobile", mobile);
        param.add("sign", param.getMd5String());
        showProgress("验证码发送中");
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissProgress();
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissProgress();
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    showToastShort(result.getInfo());
                    if (result.getStatus() == Constant.STATUS_CODE_SUCCESS) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                new CountDownTimerUtil(mTextViewForget, NetConfig.cutdown_time, 1000).start();
                            }
                        });
                    }
                }
            }
        });
    }

    /**
     * 修改密码
     */
    private void onChangePassWd() {
        final String mobile = mMobile.getText().toString();
        final String password = mPassWord.getText().toString();
        String code = mYzcode.getText().toString();
        if (CommonUtil.isEmpty(mobile)) {
            showToastShort("请输入手机号码");
            return;
        }
        if (!CommonUtil.isMobile(mobile)) {
            showToastShort("请输入正确的手机号码");
            return;
        }
        if (CommonUtil.isEmpty(code)) {
            showToastShort("请输入验证码");
            return;
        }
        if (CommonUtil.isEmpty(password)) {
            showToastShort("请输入登录密码");
            return;
        }
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.resetpassword);
        param.add("username", mMobile.getText().toString());
        param.add("password", mPassWord.getText().toString());
        param.add("verify", mYzcode.getText().toString());
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    showToastShort(result.getInfo());
                    if (result.getStatus() == 1) {
                        Intent intent = new Intent();
                        intent.putExtra(Key.USERNAME, mobile);
                        intent.putExtra(Key.PASSWORD, password);
                        setResult(RESULT_OK, intent);
                        finishActivity(true);
                    }
                }
            }
        });
    }
}
